import os, time
from Tkinter import *

class main():
    def __init__(self,parent,root):
        
        self.root = root
        self.dh = parent.dh
        self.boothPath = parent.boothPath
        self.root.title('Add Items to Existing Consignor')
        self.cons = None       
        self.mainframe = Frame(self.root,padx=20,pady=20 ) 
        self.mainframe.grid(column=0, row=0, sticky = (N, W, E ,S))
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=1)      
        self.num = StringVar()
        self.name = StringVar()
        self.addy1 = StringVar()
        self.addy2 = StringVar()
        self.addy3 = StringVar()
        self.phone = StringVar()
        self.email = StringVar()
        self.date = StringVar()
        self.timestamp = time.struct_time(time.localtime())
        self.item_num = StringVar()
        self.item_date = StringVar()
        self.item_des = StringVar()
        self.price = StringVar()
        self.status = StringVar()
        self.status.set('Enter consignor by name or number and click Search to get started')
        
        numLabel = Label(self.root,text= 'Consignor Number : ').grid(row=0,column=0,sticky=W)
        numEntry = Entry(self.root,textvariable=self.num,width=4)
        numEntry.grid(row=0,column=1,sticky=W)
        numEntry.bind('<Return>',self.readConsignorNum)
        numEntry.bind('<KP_Enter>',self.readConsignorNum)

        findByNum = Button(self.root, text='Search',command=self.readConsignorNum, width=14).grid(row=0,column=2)
        nameLabel = Label(self.root,text='Consignor Name    : ').grid(row=1,column=0,sticky=W)
        nameEntry = Entry(self.root,textvariable=self.name,width=40)
        nameEntry.grid(row=1,column=1)
        nameEntry.bind('<Return>',self.readConsignorName)
        nameEntry.bind('<KP_Enter>',self.readConsignorName)

        findByName = Button(self.root, text='Search',command=self.readConsignorName, width=14).grid(row=1,column=2)
        saveCons = Button(self.root, text = 'Save Cons. Info',width=14,command=self.saveConsInfo).grid(row=2,column=2)
        add1Label = Label(self.root,text='Address 1              : ').grid(row=2,column=0,sticky=W)
        add2Label = Label(self.root,text='Address 2              : ').grid(row=3,column=0,sticky=W)
        add3Label = Label(self.root,text='Address 3              : ').grid(row=4,column=0,sticky=W)
        add1Entry = Entry(self.root,textvariable=self.addy1,width=40).grid(row=2,column=1)
        add2Entry = Entry(self.root,textvariable=self.addy2,width=40).grid(row=3,column=1)
        add3Entry = Entry(self.root,textvariable=self.addy3,width=40).grid(row=4,column=1)
        
        phoneLabel = Label(self.root,text='Phone Number       : ').grid(row=6,column=0,sticky=W)
        phoneEntry = Entry(self.root,textvariable=self.phone,width=40).grid(row=6,column=1)
        emailLabel = Label(self.root,text='Email Address      : ').grid(row=5,column=0,sticky=W)
        emailEntry = Entry(self.root,textvariable=self.email,width=40).grid(row=5,column=1)
  
        dateLabel = Label(self.root,text='Consign Date         : ').grid(row=7,column=0,sticky=W)
        dateEntry = Entry(self.root,textvariable=self.date,width=40).grid(row=7,column=1)
        
        itemDivider = Label(self.root,text='Item Information Below').grid(row=8,column=1,sticky=W)
        itemSaveBtn = Button(self.root,text=' Save Item Info ',width=14,command=self.saveItemInfo).grid(row=8,column=2)

        itemNumLabel = Label(self.root,text='Item Number         : ').grid(row=9,column=0,sticky=W)
        itemNumEntry = Entry(self.root,textvariable=self.item_num,width=40).grid(row=9,column=1)
        descriptionLabel = Label(self.root,text='Description            : ').grid(row=10,column=0,sticky=W)
        descriptionEntry = Entry(self.root,textvariable=self.item_des,width=40)
        descriptionEntry.grid(row=10,column=1)
        itemDateLabel = Label(self.root,text='Item Date              : ').grid(row=11,column=0,sticky=W)
        itemDateEntry = Entry(self.root,textvariable=self.item_date,width=40).grid(row=11,column=1)
        itemPriceLabel = Label(self.root,text='Item Price             :$').grid(row=12,column=0,sticky=W)
        itemPriceEntry = Entry(self.root,textvariable=self.price,width=40)
        itemPriceEntry.grid(row=12,column=1)
        itemPriceEntry.bind('<Return>',self.saveItemInfo)
        itemPriceEntry.bind('<KP_Enter>',self.saveItemInfo)

        statusLabel = Label(self.root,textvariable=self.status).grid(row=13,column=0,columnspan=2)
        self.focalpoint = descriptionEntry
        doneButton = Button(self.root,text=' Done ', command=self.finalize, width=10).grid(row=13,column=2,sticky=E)
        
    def assignVars(self,cons):
        self.num.set(cons.number)
        self.name.set(cons.full_name)
        self.addy1.set(cons.addy1)
        self.addy2.set(cons.addy2)
        self.addy3.set(cons.addy3)
        self.phone.set(cons.phone_number)
        self.email.set(cons.email_address)
        self.date.set(cons.consign_date)
        self.item = self.dh.add_new_item(cons.number)
        self.item_num.set('%s' % self.item.item_number)
        self.item_date.set(self.item.consign_date)
        self.focalpoint.focus()        
    
    def readConsignorNum(self,event=None):
        num = self.num.get()
        if num in self.dh.cons_dict.keys():
            cons = self.dh.cons_dict[num]
            self.cons = cons
            self.assignVars(cons)
        else:
            self.status.set('ERROR: Consignor number not found')
            
    def readConsignorName(self,event=None):
        name = self.name.get()
        cons = self.dh.find_consignor_by_name(name)
        self.cons = cons    
        if cons == None:
            self.status.set('ERROR: No match found')
        else:
            self.status.set('Waiting for input')
            self.assignVars(cons)
                      
    def saveConsInfo(self):
        name = self.name.get()
        if len(name) > 1:
            try:
                num = '%i' % self.num.get()
            except:
                self.status.set('ERROR: Consignor number must be a number')
            cons = self.dh.cons_dict[num]
            cons.name = self.name.get()
            cons.addy1 = self.addy1.get()
            cons.addy2 = self.addy2.get()
            cons.addy3 = self.addy3.get()
            cons.email = self.email.get()
            cons.phone = self.phone.get()
            cons.date = self.date.get()
            cons.save() 
            self.status.set('Saved consignor info succesfully. Please enter item info.')
        else:
            self.status.set('Something is entered incorrectly. Check all fields for completion')

    def saveItemInfo(self,event=None):
        price = self.price.get()
        price = '%.2f' % float(price)           
        self.item.owner = self.name.get()
        self.item.description = self.item_des.get()
        self.item.consign_date = self.item_date.get()
        self.item.original_price = price
        self.item.sale_price = price
        self.item.save()
        self.item_des.set('')
        self.price.set('')
        self.readConsignorNum()
        self.status.set('Saved item successfully. Please enter the next item.')
    
    def finalize(self):
        if self.item.description == 'General Merchandise':
            self.item.delete()
        
        self.root.destroy()
            
def start(parent,root):
        
    ins = main(parent,root)
